#!/usr/bin/env python
# -*- coding: utf-8 -*-
#  member_signup_worker.py
#  description

#  Created by zhouzongli633 on 2020/8/10 5:15 下午.
#  Copyright ©Intellectus All rights reserved.


import datetime
import os
import sys
import django

CURRENT_DIR = os.path.dirname(os.path.abspath(__file__))
if CURRENT_DIR not in sys.path:
    sys.path.insert(0, CURRENT_DIR)
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'NICU2HOME.settings')
django.setup()

from Baby.models import Users
from Common.logger import gen_log
from User.models import NotificationsEdus
from User.models import CardTimes
from NICU2HOME.configs import edu_notifications


def member_signup_worker(user_id):
    user = Users.objects.filter(id=user_id).first()
    if not user:
        gen_log.info("{} user not found".format(user_id))
        return
    date = datetime.datetime.utcnow()

    save_notification_and_card_times(user.id, 293, date)

    # Send these two contents on first day of sign up
    save_notification_and_card_times(user.id, 298, date)
    save_notification_and_card_times(user.id, 347, date)
    save_notification_and_card_times(user.id, 309, date)
    save_notification_and_card_times(user.id, 88, date)
    save_notification_and_card_times(user.id, 317, date)
    save_notification_and_card_times(user.id, 97, date)
    save_notification_and_card_times(user.id, 367, date)

    # Send these two contens on second day of sign up
    save_notification_and_card_times(user.id, 341, date + datetime.timedelta(days=2))


def save_notification_and_card_times(user_id, edu_feed_id, deliver_at):
    date = datetime.datetime.utcnow()
    notification = NotificationsEdus(user_id=user_id, notification_type=edu_notifications['home_name'],
                                     edu_feed_id=edu_feed_id, deliver_at=deliver_at, sent=1, created_at=date,
                                     updated_at=date)
    card = CardTimes(card_id=edu_feed_id, user_id=user_id, notifications_edus_id=notification.id, created_at=date,
                     updated_at=date)
    notification.save()
    card.save()
